package com.jxw.launcher.util;

import android.os.Environment;
import android.util.Log;
import com.alibaba.fastjson.asm.Opcodes;
import com.jxw.gk.ControlSyncInfo;
import com.jxw.launcher.BuildConfig;
import com.jxw.launcher.MyApp;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.net.telnet.TelnetCommand;

/* loaded from: classes.dex */
public class UserData {
    private static final String CHARSET_NAME = "UTF-8";
    private static final String DEFAULT_PASSWORD = "6868";
    private static final String TAG = "UserData";
    private static final String USER_DATA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DSYSFILES/user_data.bin";
    private static final String USER1_DATA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DSYSFILES/user1_data.bin";
    private static String MASTER_PASSWORD = BuildConfig.MASTER_PWD;
    private static final int[] DATA_KEY = {142, Opcodes.INSTANCEOF, 99, 105, 108, 115, 59, 115, 101, 188, 211, 191, TelnetCommand.EOF, Opcodes.INSTANCEOF, 203, 188, 251, 207, Opcodes.ARETURN, Opcodes.ARETURN, 174, 201, TelnetCommand.NOP, 181, 196, 183, 179, 196, 213, 190, 242, 174};

    public static void setParentPassword(String str) {
        try {
            byte[] xorData = xorData(str.getBytes("UTF-8"));
            if (xorData == null) {
                Log.i(TAG, "setParentPassword(): null == data");
                return;
            }
            File file = new File(USER_DATA_PATH);
            File parentFile = file.getParentFile();
            if (parentFile != null && !parentFile.exists()) {
                parentFile.mkdirs();
            }
            if (file.exists()) {
                file.delete();
            }
            try {
                file.createNewFile();
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
                bufferedOutputStream.write(xorData);
                bufferedOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        } catch (UnsupportedEncodingException e3) {
            e3.printStackTrace();
            Log.i(TAG, "setParentPassword(): UnsupportedEncodingException");
        }
    }

    public static String getParentPassword() {
        File file = new File(USER_DATA_PATH);
        String str = "6868";
        if (!file.exists()) {
            Log.i(TAG, "getParentPassword(): !file.exists()");
            return "6868";
        }
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            int available = bufferedInputStream.available();
            if (available > 0) {
                byte[] bArr = new byte[available];
                bufferedInputStream.read(bArr);
                byte[] xorData = xorData(bArr);
                if (xorData != null) {
                    str = new String(xorData, "UTF-8");
                }
            }
            bufferedInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        return str;
    }

    public static String getMasterPassword() {
        if (ControlSyncInfo.getGKVersion(MyApp.getInstance()).equals("version6")) {
            String str = "";
            File file = new File(USER1_DATA_PATH);
            if (!file.exists()) {
                Log.i(TAG, "getParentPassword(): !file.exists()");
                return "";
            }
            try {
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                int available = bufferedInputStream.available();
                if (available > 0) {
                    byte[] bArr = new byte[available];
                    bufferedInputStream.read(bArr);
                    byte[] xorData = xorData(bArr);
                    if (xorData != null) {
                        str = new String(xorData, "UTF-8");
                    }
                }
                bufferedInputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            return str;
        }
        return MASTER_PASSWORD;
    }

    public static void setMasterPassword(String str) {
        MASTER_PASSWORD = str;
    }

    private static byte[] xorData(byte[] bArr) {
        if (bArr == null) {
            Log.i(TAG, "xorData(): null == data");
            return null;
        }
        byte[] bArr2 = (byte[]) bArr.clone();
        if (bArr2 == null) {
            Log.i(TAG, "xorData(): null == buf");
            return null;
        }
        int i = 0;
        for (int i2 = 0; i2 < bArr2.length; i2++) {
            byte b = bArr2[i2];
            int[] iArr = DATA_KEY;
            int i3 = i + 1;
            bArr2[i2] = (byte) (iArr[i] ^ b);
            i = i3 >= iArr.length ? 0 : i3;
        }
        return bArr2;
    }
}
